<?php
/**
 * User: zj
 * Date: 2017/5/6
 * Time: 17:28
 * Progress:0%
 */

namespace logic\admin\shop;


use model\Account;
use model\AccountRole;
use model\ShopStore;
use model\ShopStoreDelivery;
use think\Db;
class LShopDelivery
{
    //配送员列表
    public function index_data($param){
        $where=array();
        $status = $param['status'];
        if ($status > -1) {
            $where['sd.flag_status'] = $status;
        }
        $type = $param['type'];
        if ($type > -1) {
            $where['sd.flag_lock'] = $type;
        }
        $keywords = $param['search'];
        if (!empty($keywords)) {
            $where['a.name|ss.name|a.wechat|a.mobile'] = array('like', "%{$keywords}%"); //查询条件
        }
        $where['ar.flag_delete'] = 0;
        $where['a.flag_delete'] = 0;

        if(!empty($param['begindate'])){
            $begin_date = strtotime($param['begindate']);
        }else{
            $begin_date = '';
        }
        if(!empty($param['enddate'])){
            $end_date = strtotime('+1 day',strtotime($param['enddate']));
        }else{
            $end_date = '';
        }

        if (!empty($begin_date) && !empty($end_date)) {
            $where['ar.add_time'] = array('between', "{$begin_date},{$end_date}");
        }else if(!empty($begin_date) && empty($end_date)){
            $where['ar.add_time'] = array('egt', $begin_date);
        }else if(empty($begin_date) && !empty($end_date)){
            $where['ar.add_time'] = array('elt', $end_date);
        }

        $where['ar.role_id']=2;
        $total=DB::name('account_role ar')
            ->join('account a','a.id = ar.account_id','left')
            ->where($where)->count('ar.id');

        $page_size =$param['limit']; //获取分页数
        $start = $param['offset']; //获取开始的记录数
        //获取排序字段------------------------------------------------------------开始

        $sort_name = $param['ordinal'];
        $sort_desc = $param['order'];

        if (!empty($sort_name)) {
            $sort[$sort_name] = $sort_desc;
        } else {
            $sort['ar.add_time'] = 'desc'; //默认排序
        }
        //获取排序字段------------------------------------------------------------结束

        $rows = DB::name('account_role ar')
            ->join('account a','a.id = ar.account_id','left')
            ->field('ar.*,a.name as account_name,a.mobile as mobile,a.wechat as wechat')
            ->where($where)
            ->limit($start, $page_size)
            ->order($sort)
            ->select();
        $array=array(
            'total'=>$total,
            'rows'=>$rows
        );

        return $array;
    }


    public function delivery_data(){
        $model = new ShopStore();
        $data = $model->field("id,name")->select();
        return $data;
    }
    //编辑页面
    public function edit_data($store_id,$account_id){
        $where['ar.account_id'] = $account_id;
        $data=DB::name('account_role ar')
            ->join('shop_store_delivery sd','sd.account_id=ar.account_id','left')
            ->join('shop_store ss','ss.id = sd.store_id','left')
            ->join('account a','a.id = ar.account_id','left')
            ->field('ar.*,sd.store_id,ss.name as store_name,a.name as account_name,a.mobile as mobile,a.wechat as wechat')
            ->where($where)
            ->find();
        return $data;
    }

    //配送员信息更新
    public function  update_data($param){
        $model_account = new Account();
        Db::startTrans();
        $where['id'] = $param['account_id'];
        $data['name'] = $param['account_name'];
        $data['wechat'] = $param['wechat'];
        $data['mobile'] = $param['mobile'];
        $b =$model_account->where($where)->update($data);
        if($b!==false){
            Db::commit();
            return array(
                'success'=>true,
                'message'=>'修改成功'
            );
        }else{
            Db::rollback();
            return array(
                'success'=>false,
                'message'=>$model_account->getError()
            );
        }
    }

    public function verify_data($store_id,$account_id){
        $model = new ShopStoreDelivery();
        $where['store_id'] = $store_id;
        $where['account_id'] = $account_id;
        $data  = $model->where($where)->find();
        return $data;
    }

    public function verify_confirm_data($param){
        $model = new ShopStoreDelivery();
        $model_role = new AccountRole();
        Db::startTrans();

        $where['store_id'] = $param['store_id'];
        $where['account_id'] = $param['account_id'];
        $data['flag_status'] = $param['flag_status'];
        $data['check_time'] = time();

        $a = $model->where($where)->update($data);

        $where1['account_id'] = $param['account_id'];
        $where1['role_id'] = 3;
        $where1['flag_delete'] = 0;
        $data1['account_id'] = $param['account_id'];
        $data1['role_id'] = 3;
        $data1['flag_delete'] = 0;
        $data1['add_time'] = time();

        $b = $model_role->where($where1)->find();
        if(!empty($b)){
                $m = 1;
        }else{
            $c = $model_role->validate(true)->save($data1);
            if($c>0){
                $m = 1;
            }else{
                $m = 0;
            }
        }

        if($a!==false && $m==1){
            Db::commit();
            return array(
                'success'=>true,
                'message'=>'审核成功'
            );
        }else{
            Db::rollback();
            return array(
                'success'=>false,
                'message'=>$model->getError()
            );
        }
    }


    //配送员删除
    public function delete_data($id){
        $model=new AccountRole();
        $where['id'] = $id;
        Db::startTrans();
        $a=$model->where($where)->delete();
        if($a){
            Db::commit();
            return array(
                'success'=>true,
                'message'=>'删除成功'
            );
        }else{
            Db::rollback();
            return array(
                'success'=>false,
                'message'=>$model->getError()
            );
        }

    }



}